<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>18 继承-原型继承</title>
</head>

<body>
  <script>
    function Parent() {
      this.x = 100
      this.arr = [1, 2]
    }
    Parent.prototype.getX = function getX() {
      return this.x
    }

    function Child() {
      this.y = 200
    }

    // 写法一（不推荐使用了，效率低）
    // Child.prototype = new Parent()
    // Child.prototype.constructor = Child

    // 写法二：简单写法（推荐，相当于上面这种写法）
    Object.setPrototypeOf(Child.prototype, new Parent)

    Child.prototype.getY = function getY() {
      return this.y
    }

    let c1 = new Child()
    let c2 = new Child()

    c1.arr.push(3)

    console.log(c1) // 沿着c1的原型链就可以找到Parent上的属性和方法

    // 都发生了变化
    console.log(c1.arr) //=> [1, 2, 3]
    console.log(c2.arr) //=> [1, 2, 3]

  </script>
</body>

</html>